{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 新建批注"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from openpyxl import Workbook\n",
    "from openpyxl.comments import Comment\n",
    "from openpyxl.utils import units\n",
    "\n",
    "wb=Workbook()\n",
    "ws=wb.active\n",
    "\n",
    "comment = Comment(\"这是一条批注\", \"zhangjunhong\")\n",
    "\n",
    "#用来设置批注框的宽和高\n",
    "comment.width = 300\n",
    "comment.height = 50\n",
    "\n",
    "#给C3单元格添加批注\n",
    "ws[\"C3\"].comment = comment\n",
    "\n",
    "wb.save(r'C:\\Users\\zhangjunhong\\Desktop\\commented_book.xlsx')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 文档保护"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from openpyxl import Workbook\n",
    "wb = Workbook()\n",
    "ws = wb.active\n",
    "\n",
    "ws.protection.password = '123456'\n",
    "wb.save(r'C:\\Users\\zhangjunhongDesktop\\protection_sheet.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [],
   "source": [
    "from openpyxl import Workbook\n",
    "wb = Workbook()\n",
    "ws = wb.active\n",
    "\n",
    "wb.security.workbookPassword = '123456'\n",
    "wb.security.lockStructure = True\n",
    "wb.save(r'C:\\Users\\zhangjunhong\\Desktop\\protection_workbook.xlsx')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 冻结窗格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "from openpyxl import load_workbook\n",
    "\n",
    "wb = Workbook()\n",
    "ws = wb.active\n",
    "\n",
    "ws.freeze_panes = 'C3'  \n",
    "\n",
    "wb.save(r'C:\\Users\\zhangjunhong\\Desktop\\freeze_panes.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "from openpyxl import load_workbook\n",
    "\n",
    "wb = Workbook()\n",
    "ws = wb.active\n",
    "\n",
    "ws.freeze_panes = 'A2'  \n",
    "\n",
    "wb.save(r'C:\\Users\\zhangjunhong\\Desktop\\freeze_panes_row.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "from openpyxl import load_workbook\n",
    "\n",
    "wb = Workbook()\n",
    "ws = wb.active\n",
    "\n",
    "ws.freeze_panes = 'B1'  \n",
    "\n",
    "wb.save(r'C:\\Users\\zhangjunhong\\Desktop\\freeze_panes_col.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "789px",
    "left": "69px",
    "top": "110px",
    "width": "308.507px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
